 
package com.zdkj.subway.rca.service.impl;



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.rca.dto.in.UserDto;
import com.zdkj.subway.rca.entity.SwCkBankVerifyPayment;
import com.zdkj.subway.rca.mapper.SwCkBankVerifyPaymentMapper;
import com.zdkj.subway.rca.service.*;
import com.zdkj.subway.rca.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 银行电子账单与对账确认表汇总表
 *
 * @author stw
 * @date 2020-09-28 09:48:37
 */
@Service
public class SwCkBankVerifyPaymentServiceImpl extends ServiceImpl<SwCkBankVerifyPaymentMapper, SwCkBankVerifyPayment> implements SwCkBankVerifyPaymentService {


    @Autowired
    private SwCkStatementConfirmFacetoBankTmpService swCkStatementConfirmFacetoBankTmpService;

    @Autowired
    private SwCkStatementConfirmFacetoWechatTmpService swCkStatementConfirmFacetoWechatTmpService;

    @Autowired
    private SwCkStatementConfirmFacetoAlipayTmpService swCkStatementConfirmFacetoAlipayTmpService;

    @Autowired
    private SwCkStatementConfirmBomQrAlipayTmpService swCkStatementConfirmBomQrAlipayTmpService;

    @Autowired
    private SwCkStatementConfirmBomQrWechatTmpService swCkStatementConfirmBomQrWechatTmpService;

    @Autowired
    private SwCkStatementConfirmBankQrConsumeTmpService swCkStatementConfirmBankQrConsumeTmpService;

    @Autowired
    private SwCkStatementConfirmTvmAlipayTmpService swCkStatementConfirmTvmAlipayTmpService;

    @Autowired
    private SwCkStatementConfirmTvmWechatTmpService swCkStatementConfirmTvmWechatTmpService;

    @Autowired
    private SwCkStatementConfirmTvmBankTmpService swCkStatementConfirmTvmBankTmpService;

    @Autowired
    private SwCkStatementConfirmBankQuickpassQrConsumeTmpService swCkStatementConfirmBankQuickpassQrConsumeTmpService;

    @Autowired
    private SwCkStatementConfirmAlipayQrConsumeTmpService swCkStatementConfirmAlipayQrConsumeTmpService;

    @Autowired
    private SwCkStatementConfirmWechatQrConsumeTmpService swCkStatementConfirmWechatQrConsumeTmpService;

    @Autowired
    private SwCkStatementConfirmAirQrConsumeTmpService swCkStatementConfirmAirQrConsumeTmpService;

    @Autowired
    private SwCkStatementCatReplenishTmpService swCkStatementCatReplenishTmpService;

    @Autowired
    private SwCkStatementCatSubwayconsumeTmpService swCkStatementCatSubwayconsumeTmpService;

    @Autowired
    private SwCkStatementJtyktSubwayconsumeTmpService swCkStatementJtyktSubwayconsumeTmpService;

    @Autowired
    private SwCkClearingConfirmChanganCardTmpService swCkClearingConfirmChanganCardTmpService;

    @Autowired
    private SwCkClearingConfirmTrafficCardTmpService swCkClearingConfirmTrafficCardTmpService;

    @Autowired
    private SwCkStatementCatXaztReplenishTmpService swCkStatementCatXaztReplenishTmpService;

    @Autowired
    private SwCkStatementConfirmAppQrConsumeTmpService swCkStatementConfirmAppQrConsumeTmpService;

    @Autowired
    private SwCkStatementConfirmAppWechatTmpService swCkStatementConfirmAppWechatTmpService;

    @Autowired
    private SwCkStatementConfirmDailyticketCatConsumeTmpService swCkStatementConfirmDailyticketCatConsumeTmpService;

	@Autowired
    private  SwCkStatementConfirmAlipayQrConnectivityTmpService  swCkStatementConfirmAlipayQrConnectivityTmpService;
	@Autowired
    private  SwCkStatementConfirmWechatQrConnectivityTmpService  swCkStatementConfirmWechatQrConnectivityTmpService;

	@Autowired
    private SwCkStatementConfirmBomPosTmpService swCkStatementConfirmBomPosTmpService;

    @Override
    public List<Map<String,String>> getSwCkBankVerifyPaymentConfirmTableListPre() {
        List<Map<String,String>> list = this.baseMapper.getSwCkBankVerifyPaymentConfirmTableListPre();
        return list;
    }

    @Override
    public void exportTamplates(String verfiyConfirmFlag, HttpServletResponse response) {
        Map<String,String> map = this.baseMapper.getSwCkBankVerifyPaymentConfirmTable(verfiyConfirmFlag);
        String verifyConfirmName = map.get("verifyConfirmName");
        String tableName = map.get("tableName");

        BufferedInputStream br = null;
        OutputStream out = null;
        try {
            response.reset(); // 非常重要
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            final String fileName = URLEncoder.encode(verifyConfirmName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");

            br =  new BufferedInputStream(this.getClass().getResourceAsStream("/static/excel/template/import/" + tableName + ".xlsx"));

            byte[] buf = new byte[1024];
            int len = 0;

            out = response.getOutputStream();
            while ((len = br.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }

    @Override
    public Boolean saveBatchWithExcel(MultipartFile file, String verfiyConfirmFlag) throws Exception {

        File excelFile = MultipartFileToFile.multipartFileToFile(file);
        if("1".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmFacetoBankTmpService.saveBatchWithExcel(excelFile);
        } else if("2".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmFacetoWechatTmpService.saveBatchWithExcel(excelFile);
        } else if("3".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmFacetoAlipayTmpService.saveBatchWithExcel(excelFile);
        }  else if("4".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmBomQrAlipayTmpService.saveBatchWithExcel(excelFile);
        }  else if("5".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmBomQrWechatTmpService.saveBatchWithExcel(excelFile);
        }  else if("6".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmTvmAlipayTmpService.saveBatchWithExcel(excelFile);
        }  else if("7".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmTvmWechatTmpService.saveBatchWithExcel(excelFile);
        }  else if("8".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmTvmBankTmpService.saveBatchWithExcel(excelFile);
        } else if("9".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmBankQrConsumeTmpService.saveBatchWithExcel(excelFile);
        }  else if("10".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmBankQuickpassQrConsumeTmpService.saveBatchWithExcel(excelFile);
        } else if("11".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmAlipayQrConsumeTmpService.saveBatchWithExcel(excelFile);
        } else if("12".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmWechatQrConsumeTmpService.saveBatchWithExcel(excelFile);
        }  else if("13".equals(verfiyConfirmFlag)) {
            swCkStatementCatXaztReplenishTmpService.saveBatchWithExcel(excelFile);
            //swCkStatementConfirmAirQrConsumeTmpService.saveBatchWithExcel(excelFile);
        }  else if("14".equals(verfiyConfirmFlag)) {
            swCkStatementCatReplenishTmpService.saveBatchWithExcel(excelFile);
        }  else if("15".equals(verfiyConfirmFlag)) {
            swCkStatementCatSubwayconsumeTmpService.saveBatchWithExcel(excelFile);
        }  else if("16".equals(verfiyConfirmFlag)) {
            swCkStatementJtyktSubwayconsumeTmpService.saveBatchWithExcel(excelFile);
        }  else if("17".equals(verfiyConfirmFlag)) {
            swCkClearingConfirmChanganCardTmpService.saveBatchWithExcel(excelFile);
        }  else if("18".equals(verfiyConfirmFlag)) {
            swCkClearingConfirmTrafficCardTmpService.saveBatchWithExcel(excelFile);
        }  else if("19".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmAppQrConsumeTmpService.saveBatchWithExcel(excelFile);
        }  else if("20".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmDailyticketCatConsumeTmpService.saveBatchWithExcel(excelFile);
        }  else if("21".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmAlipayQrConnectivityTmpService.saveBatchWithExcel(excelFile);
        }  else if("22".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmWechatQrConnectivityTmpService.saveBatchWithExcel(excelFile);
        }  else if("23".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmAppWechatTmpService.saveBatchWithExcel(excelFile);
        }  else if("24".equals(verfiyConfirmFlag)) {
            swCkStatementConfirmBomPosTmpService.saveBatchWithExcel(excelFile);
        }

        SwCkBankVerifyPayment swCkBankVerifyPayment = new SwCkBankVerifyPayment();

        PigxUser pigxUser = SecurityUtils.getUser();
        UserDto user = new UserDto();
        if(pigxUser == null){
            user = UserUtil.userDto();
        }else {
            user = UserUtil.pigxUser();
        }
        swCkBankVerifyPayment.setDeptId(user.getDeptId());
        swCkBankVerifyPayment.setCreatorId(user.getId());
        swCkBankVerifyPayment.setImportCreatorId(user.getId());
        swCkBankVerifyPayment.setCreatorName(user.getNikeName());
        swCkBankVerifyPayment.setImprotCreatorName(user.getNikeName());
        swCkBankVerifyPayment.setImportCreatorCode(user.getUserCode());
        swCkBankVerifyPayment.setImportTime(new Date().toInstant().atZone( ZoneId.systemDefault() ).toLocalDate());
        swCkBankVerifyPayment.setVerfiyConfirmFlag(verfiyConfirmFlag);
		
        Map<String,String> mapPaymentConfirm = this.baseMapper.getSwCkBankVerifyPaymentConfirmTable(verfiyConfirmFlag);

		//根据标识获取表名,并追加_tmp表名，查找最大最小日期
		String tableName = mapPaymentConfirm.get("tableName");
		Map<String,String> mapBetweenDate = this.baseMapper.selectMaxMinStatisticsDate(tableName+"_tmp",user.getId());
		if(mapBetweenDate != null) {
            String minStatisticsDate = mapBetweenDate.get("minStatisticsDate");
            String maxStatisticsDate = mapBetweenDate.get("maxStatisticsDate");
            swCkBankVerifyPayment.setBeginTime(LocalDate.parse(minStatisticsDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
			swCkBankVerifyPayment.setEndTime(LocalDate.parse(maxStatisticsDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        swCkBankVerifyPayment.setVerifyConfirmName(mapPaymentConfirm.get("verifyConfirmName"));

        swCkBankVerifyPayment.setImportStatus("0");
        this.baseMapper.insert(swCkBankVerifyPayment);

        return true;
    }

    /**
     * 获取文件存储路径
     * @return
     */
    @Override
    public List<Map<String, String>> getSwCkAutoUploadFileResolveListPre() {
        List<Map<String,String>> list = this.baseMapper.getSwCkAutoUploadFileResolveListPre();
        return list;
    }

    @Override
    public List<Map<String,String>> getSwCkBankVerifyPaymentConfirmTableList() {
        PigxUser user = SecurityUtils.getUser();
        return this.baseMapper.getSwCkBankVerifyPaymentConfirmTableList(user.getId());
    }
}
